package com.lzhsite.technology.designPattern.composite.FolderFileImplement;

import java.util.ArrayList;
import java.util.List;

public class Folder implements Root {
	private String name;
	private List<Root> folders;

	public Folder(String name) {
		this.name = name;
		this.folders = new ArrayList<Root>();
	}

	public boolean addfile(Root file) {
		return folders.add(file);
	}

	public boolean removeFile(Root file) {
		return folders.remove(file);
	}

	public List<Root> getFiles() {
		return folders;
	}

	
	
	public String getName() {
		return name;
	}

	private static String getLevelStr(int level) {
		StringBuffer levelStr = new StringBuffer();
		for (int levelI = 0; levelI < level; levelI++) {
			levelStr.append("\t");
		}
		return levelStr.toString();
	}

    private int level = 0;
    
	public void printFileName(Root root) {

		if (root instanceof Folder) {
			System.out.print(getLevelStr(level));
			System.out.print("   |   ");
			System.out.println(((Folder) root).getName());
			for (Root sonRoot : root.getFiles()) {
				level++;
				printFileName(sonRoot);
				level--;
			}
			
		} else if (root instanceof File) {
			System.out.print(getLevelStr(level));
			root.display();
			return;
		}
	}

	public void display() {
		System.out.println(name);
		for (Root root : folders) {
			printFileName(root);
		}
	}
}
